package com.wan37.client.message.received;

import com.google.protobuf.Message;
import com.googlecode.protobuf.format.JsonFormat;
import com.wan37.client.handler.ClientHandler;
import com.wan37.client.service.cache.TradingShopCache;
import com.wan37.protobuf.constant.CommonTradingShop;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.GameTradingShop;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.LinkedList;

/**
 * @author : luoyong
 * @date : 2020-08-05 16:59
 **/
@Component(PtoNum.TRADINGSHOPRESPONSE + "")
public class TradingShopResponse implements ReceiveMsg{
    private final Logger logger = LoggerFactory.getLogger(TradingShopResponse.class);
    @Autowired
    private TradingShopCache tradingShopCache;
    private ClientHandler clientHandler = ClientHandler.getInstance();
    @Override
    public void execute(Message msg) {
        GameTradingShop.TradingShopResponse tradingShopResponse = (GameTradingShop.TradingShopResponse) msg;
        int code = tradingShopResponse.getCode();
        switch (code){
            case CommonTradingShop.GET_ALL:
                for (GameTradingShop.TradingShop tradingShop : tradingShopResponse.getTradingShopList()){
                    TradingShopCache.TradingShopIn tradingShopIn = tradingShopCache.new TradingShopIn();
                    tradingShopIn.setTradingShop(tradingShop);
                    tradingShopIn.setTradingShopRecordList(new LinkedList<>());
                    tradingShopCache.getTradingShopInMap().put(tradingShop.getTradingShopId(), tradingShopIn);
                }
                for (GameTradingShop.TradingShopRecord tradingShopRecord :
                        tradingShopResponse.getTradingShopRecordList()){
                    tradingShopCache.getTradingShopInMap().get(tradingShopRecord.getTradingShopId())
                            .getTradingShopRecordList().add(tradingShopRecord);
                }
                break;
            case CommonTradingShop.ADD_FIXED_PRICE:
            case CommonTradingShop.ADD_AUCTION:
                TradingShopCache.TradingShopIn tradingShopIn = tradingShopCache.new TradingShopIn();
                tradingShopIn.setTradingShop(tradingShopResponse.getTradingShop(0));
                tradingShopIn.setTradingShopRecordList(new LinkedList<>());
                tradingShopCache.getTradingShopInMap().put(tradingShopResponse
                        .getTradingShop(0).getTradingShopId(), tradingShopIn);
                logger.info(tradingShopResponse.getString());
                break;
            case CommonTradingShop.BUY_AUCTION:
            case CommonTradingShop.BUY_FIXED_PRICE:
            case CommonTradingShop.ERROR:
                logger.info(tradingShopResponse.getString());
                break;
            case CommonTradingShop.GET_USER_ALL_BUY_RECORD:
            case CommonTradingShop.GET_USER_ALL_USER_ID_RECORD:
                for (GameTradingShop.TradingShopRecord tradingShopRecord : tradingShopResponse.getTradingShopRecordList()){
                    logger.info(JsonFormat.printToString(tradingShopRecord));
                }
                break;
            default:
                break;
        }
    }
}
